<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>onsubmit练习</title>
    <script type="text/javascript">
    window.onload = function () {
      //1. 获取dom对象
      var form2 = document.getElementById("form2");
      //2. 绑定
      form2.onsubmit = function () {
        if (!(form2.username.value.length >= 4 && form2.username.value.length <= 6)) {
          alert("用户名长度必须为4-6个字符");
          return false;
        }
        if (form2.pwd.value.length != 6) {
          alert("密码长度(6)");
          return false;//不提交
        }
        if (form2.pwd.value != form2.pwd2.value) {
          alert("两次密码不等");
          return false;
        }
        var emailPatt = /^[\w-]+@([a-zA-Z]+\.)+[a-zA-Z]+$/;
        if (!emailPatt.test(form2.email.value)) {
          //4 提示用户
          alert("电子邮件格式不正确~")
          return false;
        }
        return true;
      }
    }
    </script>
</head>
<body>
<h1>注册用户</h1> <!-- 动态注册表单提交事件 -->
<form action="ok.html" id="form2">
  用户名: <input type="text" name="username"/>长度(4-6)<br/>
  密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码: <input type="password" name="pwd"/>长度(6)<br/>
  确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认: <input type="password" name="pwd2">长度(6)<br/>
  电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;邮: <input type="text" name="email">满足基本格式<br/>
  <input type="submit" value="注册用户"/></form>
</body>
</html>